class Post < ActiveRecord::Base  
  attr_accessor :new_topic
  belongs_to :topic
  belongs_to :user
  belongs_to :updated_by_user, :foreign_key => 'updated_by', :class_name => 'User'
  
  validates_presence_of :content
  validates_presence_of :user_id
  validates_presence_of :topic_id
  
  def before_save
    self.content_html = self.content # TODO convert bbcode in html
  end
  
  def after_create
    unless new_topic
      self.topic.replied_by = self.user_id
      self.topic.replied_at = Time.now
      self.topic.increment(:posts_count)
      self.topic.save
      Forum.update(self.topic.forum_id, :last_reply_id => self.id)
      Forum.increment_counter(:posts_count, self.topic.forum_id)
    end
  end
end
